package com.jwds.digital.bios.business.service.service.belt.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jwds.digital.bios.business.service.service.belt.IBeltRouteTargetHisService;
import com.jwds.digital.bios.business.core.infrastructure.dto.belt.BeltRouteTargetHisReqDTO;
import com.jwds.digital.bios.business.core.infrastructure.vo.belt.BeltRouteTargetHisRespVO;
import com.jwds.digital.bios.business.core.infrastructure.dto.belt.assembler.BeltRouteTargetHisAssembler;
import com.jwds.digital.bios.business.core.infrastructure.entity.belt.BeltRouteTargetHis;
import com.jwds.digital.bios.business.core.infrastructure.mapper.belt.BeltRouteTargetHisMapper;
import com.jwds.digital.bios.business.core.exceptions.RecordNotFoundException;
import com.jwds.tdc.framework.application.lib.page.PageRequest;
import com.jwds.tdc.framework.application.lib.page.PageResponse;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 实现类
 * @author 林柏云
 * @date 2024-12-11
 */
@Slf4j
@Service
public class BeltRouteTargetHisServiceImpl extends ServiceImpl<BeltRouteTargetHisMapper, BeltRouteTargetHis> implements IBeltRouteTargetHisService {

  @Autowired
  private BeltRouteTargetHisMapper baseMapper;

  @Override
  public BeltRouteTargetHisRespVO queryById(Long id) {
    if(id == null) {
      return null;
    }
    BeltRouteTargetHis entity = baseMapper.selectById(id);
    return BeltRouteTargetHisAssembler.entity2dto(entity);
  }

  @Override
  public PageResponse<BeltRouteTargetHisRespVO> page(PageRequest<BeltRouteTargetHisReqDTO> request) {
    BeltRouteTargetHisReqDTO condition = request.getQueryCondition();
    Page<BeltRouteTargetHisRespVO> page = new Page<>(request.getPageIndex(), request.getPageSize());
    IPage<BeltRouteTargetHisRespVO> pageData = baseMapper.pageQuery(page, condition);
    return new PageResponse<>((Page<?>)pageData, pageData.getRecords());
  }

  @Override
  public List<BeltRouteTargetHisRespVO> list(BeltRouteTargetHisReqDTO reqDto) {
    return baseMapper.queryList(reqDto);
  }

  @Override
  public void add(BeltRouteTargetHisReqDTO reqDto) {
    BeltRouteTargetHis entity = BeltRouteTargetHisAssembler.dto2entity(reqDto);
    baseMapper.insert(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void batchAdd(List<BeltRouteTargetHisReqDTO> dtoList) {
    dtoList.forEach(this::add);
  }

  @Override
  public void update(BeltRouteTargetHisReqDTO reqDto) {
    BeltRouteTargetHis oldEntity = baseMapper.selectById(reqDto.getId());
    if (oldEntity == null) {
      throw new RecordNotFoundException();
    }
    BeltRouteTargetHis newEntity = BeltRouteTargetHisAssembler.dto2entity(reqDto, oldEntity);
    baseMapper.updateById(newEntity);
  }

  @Override
  public void delete(Long id) {
    BeltRouteTargetHis oldEntity = baseMapper.selectById(id);
    if (oldEntity == null) {
      throw new RecordNotFoundException();
    }
    baseMapper.deleteById(id);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void batchDelete(List<Long> ids) {
    for (Long id : ids) {
      delete(id);
    }
  }

}